package com.hightrast.service.redis;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

//@Service
public class RedisService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    public static final long DEFAULT_EXPIRE = 86400L;
    public static final long NOT_EXPIRE = -1L;

    public RedisService() {
    }

    public Set<String> getKeys(String keys) {
        return this.redisTemplate.keys(keys);
    }

    public boolean setIfAbsent(String key, Object value, long time, TimeUnit timeUnit) {
        return this.redisTemplate.opsForValue().setIfAbsent(key, value, time, timeUnit);
    }

    public boolean setIfAbsent(String key, Object value) {
        return this.redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    public long incr(String key, long delta, long time) {
        if (delta < 0L) {
            throw new RuntimeException("递增因子必须大于0");
        } else {
            long o = this.redisTemplate.opsForValue().increment(key, delta);
            this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
            return o;
        }
    }

    public boolean existsKey(String key) {
        return this.redisTemplate.hasKey(key);
    }

    public void renameKey(String oldKey, String newKey) {
        this.redisTemplate.rename(oldKey, newKey);
    }

    public boolean renameKeyNotExist(String oldKey, String newKey) {
        return this.redisTemplate.renameIfAbsent(oldKey, newKey);
    }

    public void deleteKey(String key) {
        this.redisTemplate.delete(key);
    }

    public void deleteKey(String... keys) {
        Set<String> kSet = (Set) Stream.of(keys).map((k) -> {
            return k;
        }).collect(Collectors.toSet());
        this.redisTemplate.delete(kSet);
    }

    public void deleteKey(Collection<String> keys) {
        Set<String> kSet = (Set)keys.stream().map((k) -> {
            return k;
        }).collect(Collectors.toSet());
        this.redisTemplate.delete(kSet);
    }

    public void expireKey(String key, long time, TimeUnit timeUnit) {
        this.redisTemplate.expire(key, time, timeUnit);
    }

    public void expireKeyAt(String key, Date date) {
        this.redisTemplate.expireAt(key, date);
    }

    public long getKeyExpire(String key, TimeUnit timeUnit) {
        return this.redisTemplate.getExpire(key, timeUnit);
    }

    public void persistKey(String key) {
        this.redisTemplate.persist(key);
    }

    public Object get(String key) {
        return key == null ? null : this.redisTemplate.opsForValue().get(key);
    }

    public boolean set(String key, Object value) {
        try {
            this.redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    public boolean set(String key, Object value, long time) {
        try {
            if (time > 0L) {
                this.redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                this.set(key, value);
            }

            return true;
        } catch (Exception var6) {
            var6.printStackTrace();
            return false;
        }
    }

    public boolean setValue(String key, Object value) {
        try {
            this.redisTemplate.opsForValue().set(key, value, 0L);
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    public long incr(String key, long delta) {
        if (delta < 0L) {
            throw new RuntimeException("递增因子必须大于0");
        } else {
            return this.redisTemplate.opsForValue().increment(key, delta);
        }
    }

    public long decr(String key, long delta) {
        if (delta < 0L) {
            throw new RuntimeException("递减因子必须大于0");
        } else {
            return this.redisTemplate.opsForValue().increment(key, -delta);
        }
    }

    public Object hget(String key, String item) {
        return this.redisTemplate.opsForHash().get(key, item);
    }

    public List<Object> hMultiGetGet(String key, Collection<Object> collection) {
        return collection != null && collection.size() > 0 ? this.redisTemplate.opsForHash().multiGet(key, collection) : this.redisTemplate.opsForHash().values(key);
    }

    public Map<Object, Object> hmget(String key) {
        return this.redisTemplate.opsForHash().entries(key);
    }

    public boolean hmset(String key, Map<String, Object> map) {
        try {
            this.redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    public boolean hmset(String key, Map<String, Object> map, long time) {
        try {
            this.redisTemplate.opsForHash().putAll(key, map);
            if (time > 0L) {
                this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }

            return true;
        } catch (Exception var6) {
            var6.printStackTrace();
            return false;
        }
    }

    public boolean hset(String key, String item, Object value) {
        try {
            this.redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
    }

    public boolean hset(String key, String item, Object value, long time) {
        try {
            this.redisTemplate.opsForHash().put(key, item, value);
            if (time > 0L) {
                this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }

            return true;
        } catch (Exception var7) {
            var7.printStackTrace();
            return false;
        }
    }

    public void hdel(String key, Object... item) {
        this.redisTemplate.opsForHash().delete(key, item);
    }

    public boolean hHasKey(String key, String item) {
        return this.redisTemplate.opsForHash().hasKey(key, item);
    }

    public double hincr(String key, String item, double by) {
        return this.redisTemplate.opsForHash().increment(key, item, by);
    }

    public double hdecr(String key, String item, double by) {
        return this.redisTemplate.opsForHash().increment(key, item, -by);
    }

    public Long addToSet(String key, Object value) {
        Long aLong = this.redisTemplate.opsForSet().add(key, new Object[]{value});
        return aLong;
    }

    public boolean addToZset(String key, Object value, double score) {
        return this.redisTemplate.opsForZSet().add(key, value, score);
    }

    public Set<Object> getZset(String key, long beg, long end) {
        return this.redisTemplate.opsForZSet().range(key, beg, end);
    }

    public Long addToList(String key, Object value) {
        Long aLong = this.redisTemplate.opsForList().leftPush(key, value);
        return aLong;
    }

    public synchronized Long getSizeToList(String key) {
        return this.redisTemplate.opsForList().size(key);
    }

    public synchronized Object popToList(String key) {
        return this.redisTemplate.opsForList().rightPop(key);
    }

    public Set<Object> members(String key) {
        Set<Object> members = this.redisTemplate.opsForSet().members(key);
        return members;
    }

    public Long removeObjectFromSet(String key, Object object) {
        Long remove = this.redisTemplate.opsForSet().remove(key, new Object[]{object});
        return remove;
    }

    public Long removeObjectFromZSet(String key, Object object) {
        Long remove = this.redisTemplate.opsForZSet().remove(key, new Object[]{object});
        return remove;
    }

    public List<Object> multiGet(Collection<String> keys) {
        return this.redisTemplate.opsForValue().multiGet(keys);
    }
}